#include "filetrap.h"
#include "ui_filetrap.h"
#include "mainobject.h"

FileTrap::FileTrap(QWidget *parent) :
    QDialog(parent, Qt::WindowStaysOnTopHint
              | Qt::FramelessWindowHint
              | Qt::Tool),
    m_ui(new Ui::FileTrap)
{
    m_ui->setupUi(this);
    mover = new WindowMover(this, EN_FILE_POS);
    installEventFilter(this);
}

FileTrap::~FileTrap()
{
    delete mover;
    delete m_ui;
}

void FileTrap::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

bool FileTrap::eventFilter(QObject *object, QEvent *event){
    return mover->eventFilter(object, event);
}

void FileTrap::show(){
    QSettings settings;
    int x = settings.value(QString(EN_FILE_POS)+QString("_X"), 0).toInt();
    int y = settings.value(QString(EN_FILE_POS)+QString("_Y"), 0).toInt();

    move(x, y);
    QDialog::show();
    QRect screen = QApplication::desktop()->screenGeometry(this);
    qDebug() << x << y << screen.x() << screen.width() << width() << height();
    if(screen.x()>=x || screen.y()>=y
       || screen.x()+screen.width()<=x+width()
       || screen.y()+screen.height()<=y+height()){
        x = screen.width()-width()-EN_FILE_SHIFT+screen.x();
        y = screen.height()-height()-EN_FILE_SHIFT+screen.y();
    }
    move(x, y);

    settings.setValue(EN_FILE_VISIBLE, true);
}

void FileTrap::hide(){
    QDialog::hide();
    QSettings settings;
    settings.setValue(EN_FILE_VISIBLE, false);
}

void FileTrap::keyPressEvent(QKeyEvent *evt){
    if(evt->key()==Qt::Key_Escape){
        evt->ignore();
    }
}
